package com.ejiaoyi.common.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 文件操作工具类
 *
 * @author unknownChivalrous
 * @since 2020/3/30
 */
public class FileUtil {

    private static String rootPath;

    private static final String FILE_POINT = ".";

    public static String getRootPath() {
        return rootPath;
    }

    public static void setRootPath(String rootPath) {
        FileUtil.rootPath = rootPath;
    }

    public static String getClassPath() {
        try {
            return ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取本地文件生成目录
     *
     * @return 本地文件生成目录
     */
    public static String getCustomFilePath() {
        try {
            return rootPath + "\\custom-file\\";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过字节大小 计算可读性大小 自动匹配单位
     *
     * @param byteSize 字节大小
     * @return 文件可读大小
     */
    public static String getReadSize(long byteSize) {
        int decimal = 1024;
        double readSize = CalcUtil.divide(byteSize, decimal);
        String unit = "KB";

        if (readSize > decimal) {
            readSize = CalcUtil.divide(readSize, decimal);
            unit = "MB";
            if (readSize > decimal) {
                readSize = CalcUtil.divide(readSize, decimal);
                unit = "GB";
                if (readSize > decimal) {
                    readSize = CalcUtil.divide(readSize, decimal);
                    unit = "TB";
                }
            }
        }
        return readSize + unit;
    }

    /**
     * 获取文件后缀名
     *
     * @param fileName 文件名
     * @return 文件后缀名
     */
    public static String getSuffix(String fileName) {
        String suffix = "";

        if (StringUtils.isNotEmpty(fileName)) {
            int index = fileName.lastIndexOf(".");

            if (index > 0 && index < fileName.length() - 1) {
                suffix = fileName.substring(index + 1);
            }
        }

        return suffix;
    }

    /**
     * 获取文件的MD5
     *
     * @param path 文件路径
     * @return 文件MD5
     */
    public static String getMD5(String path) throws IOException {
        return DigestUtils.md5Hex(new FileInputStream(path));
    }

    /**
     * 获取文件的SHA1
     *
     * @param path 文件路径
     * @return 文件SHA1
     */
    public static String getSHA1(String path) throws IOException {
        return DigestUtils.sha1Hex(new FileInputStream(path));
    }

    /**
     * 创建文件
     *
     * @param path 文件路径
     * @throws IOException IO流异常
     */
    public static boolean createFile(String path) throws IOException {
        File file = new File(path);

        if (!file.exists()) {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            return file.createNewFile();
        }

        return true;
    }

    /**
     * 创建目录
     *
     * @param path 文件路径
     */
    public static boolean createDir(String path) {
        if (StringUtils.contains(path, ".")) {
            path = new File(path).getParentFile().getPath();
        }

        File file = new File(path);

        if (!file.exists()) {
            return file.mkdirs();
        } else {
            return true;
        }
    }
}
